package vis.ds.stack;

import processing.core.PApplet;
import vis.app.VisualObject;

public class Stack extends VisualObject {
	public static int STACK_SIZE = 6;
	public static int STACK_WIDTH = StackElem.ELEM_SIZE;
	public static int STACK_HEIGHT = STACK_SIZE * StackElem.ELEM_SIZE;
	public static int BOTTOM_MARGIN = 50;

	private int initX = 0;
	private int initY = 0;
	private int x = 0;
	private int y = 0;
	private int color = 0;

	private int stackTop = 0;
	private StackElem[] data;
	
	public Stack(PApplet parent) {
		super(parent);

		initDisplay();
		initStack();
	}

	private void initDisplay() {
		initX = (p.width - StackElem.ELEM_SIZE) / 2;
		// initX = p.width / 2 - StackElem.ELEM_SIZE / 2
		initY = p.height - STACK_HEIGHT - BOTTOM_MARGIN;

		x = initX;
		y = initY;

		color = 255;
	}
	
	private void initStack() {
		stackTop = 0;
		data = new StackElem[STACK_SIZE];

		push(9);
		push(22);
	}
	
	@Override
	public void update() {
		_updateStackElements();
	}

	private void _updateStackElements() {
		for (int i = 0; i < stackTop; ++i) {
			data[i].update();
		}
	}

	@Override
	public void display() {
		p.fill(color);
		{
			_displayStackFrame();
			_displaySeperateLine();
			_displayStackElements();
		}
		p.noFill();
	}

	private void _displayStackFrame() {
		p.rect(x, y, STACK_WIDTH, STACK_HEIGHT);
	}

	private void _displaySeperateLine() {
		for (int i = 1; i < STACK_SIZE; ++i) {
			int fromX = initX;
			int fromY = initY + i * StackElem.ELEM_SIZE;
			int toX = initX + StackElem.ELEM_SIZE;
			int toY = fromY;
			p.line(fromX, fromY, toX, toY);
		}
	}
	
	private void _displayStackElements() {
		for (int i = 0; i < stackTop; ++i) {
			data[i].display();
		}
	}

	/*
	 * STACK FUNCTIONS
	 */
	
	public boolean isFull() {
		return stackTop == STACK_SIZE - 1;
	}
	
	public void push(int value) {
		System.out.println("Pushing");
		StackElem stackElem = new StackElem(p, stackTop, value);
		data[stackTop] = stackElem;
		stackTop++;
	}

	public int pop() {
		return 0;
	}

}
